* getting the sentiment proxy 

*winsor2	rvc rvj cojump, replace cuts (1 99)

gen firm = int((mod(_n-1, 18120))/120) +1
sort firm
by firm: egen trend = seq (), f(1) t(120)

tsset firm trend

* --------------- Sentiment Proxy for each firm---------------------

gen fitted_values = .

levelsof firm, local(firms)
foreach firm in `firms' {
    reg returns rjv cojumps if firm_id == `firm'
    predict temp_fitted if firm == `firm'
    replace fitted_values = temp_fitted if firm_id == `firm'
    drop temp_fitted
}

rename fitted_values sentiment

* Buy initiated trades==1 (positive returns) sell==0 (negative returns)
gen buy=cds_return>0 

* Large investors==1 Extrapolators==0
gen large= volume>18.01

* Sentiment proxy: sentiment==jumps; sentiment1=jumps+cojumps; sentiment2=jumps+cojumps+liquidity

*reg cds_return rjv , vce(cluster firm)
*predict fitted
*rename fitted sentiment

*reg d.cds rj , vce(cluster firm)
*predict fitted
*rename fitted sentiment_a

*********reg cds_return rjv cojumps // this is the one used in Oct.2024
*********predict fitted
*********rename fitted sentiment1 

*reg cds_return rjv cojumps liquidity , vce(cluster firm)
*predict fitted
*rename fitted sentiment2 

* Get my sentiment proxy-**I say that I take changes(actually just lags) and also controle for roll shock**
*reg d.cds l.rvc l.rvj l.cojump l.illiq, vce(cluster firm)
*predict fitted

* ---------- Predict CDS Returns in the spirit of MS paper -------------------
reg d.cds l.SENTIMENT
estimates store m1

reg d.cds l.NEWS, vce(cluster firm)
estimates store m2

reg d.cds l.EQ_SENTIMENT, vce(cluster firm)
estimates store m3

reg d.cds l.ANALYST, vce(cluster firm)
estimates store m4

reg d.cds l.analy_disag, vce(cluster firm)
estimates store m5

reg d.cds l.SENTIMENT l.NEWS l.EQ_SENTIMENT l.ANALYST l.analy_disag, vce(cluster firm)
estimates store m6

reg d.cds l.SENTIMENT l.TERM_SPREAD l.DEF ROE DIVIDEND LEVERAGE,vce(cluster firm) 
estimates store m7

reg d.cds l.SENTIMENT l.SP500 l.VIX l.SHORT_RATE l.TERM_SPREAD l.DEF ROE DIVIDEND LEVERAGE, vce(cluster firm)
estimates store m8

estout m1 m2 m3 m4 m5 m6 m7 m8  using ${path_table}RET_NEW.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7 _est_m8  

* ------------------- This is my cross-sectional (Famma-McBeth) of CDS ret on sentiment and other stuff..V.large coefficients with ,be!!
xtreg d.cds l.SENTIMENT, be
estimates store m1

xtreg d.cds l.NEWS, be
estimates store m2

xtreg d.cds l.EQ_SENTIMENT, be
estimates store m3

xtreg d.cds l.ANALYST, be
estimates store m4

xtreg d.cds l.analy_disag, be
estimates store m5

xtreg d.cds l.SENTIMENT l.NEWS l.EQ_SENTIMENT l.ANALYST l.analy_disag, be
estimates store m6

xtreg d.cds l.SENTIMENT l.VIX l.SHORT_RATE l.TERM_SPREAD l.DEF l.EQ_RETURN ROE DIVIDEND LEVERAGE, be
estimates store m7

estout m1 m2 m3 m4 m5 m6 m7   using ${path_table}RET_CS.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7 

   
* ------------------- Famma-McBeth predictive regressions -----------------
*xtfmb return SENTIMENT
*estimates store m1

*xtfmb return NEWS
*estimates store m2

*xtfmb return EQ_SENTIMENT
*estimates store m3

*xtfmb return ANALYST
*estimates store m4

*xtfmb return analy_disag
*estimates store m5

*xtfmb return SENTIMENT NEWS EQ_SENTIMENT ANALYST analy_disag
*estimates store m6

*xtfmb return SENTIMENT TERM_SPREAD DEF ROE DIVIDEND LEVERAGE 
*estimates store m7

*xtfmb return SENTIMENT SP500 VIX SHORT_RATE TERM_SPREAD DEF ROE DIVIDEND LEVERAGE
*estimates store m8

*estout m1 m2 m3 m4 m5 m6 m7 m8  using ${path_table}FMB.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
*drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7 _est_m8

* ---------- Predict changes in Financing mix: firm specific data --------------
reg d.ndi d.SENTIMENT, vce(robust)
estimates store m1

reg d.ndi d.NEWS, vce(robust)
estimates store m2

reg d.ndi d.EQ_SENTIMENT, vce(robust)
estimates store m3

reg d.ndi d.ANALYST, vce(robust)
estimates store m4

reg d.ndi d.analy_disag, vce(robust)
estimates store m5

reg d.ndi d.SENTIMENT d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag , vce(robust)
estimates store m6

reg d.ndi d.SENTIMENT d.VIX d.SHORT_RATE d.TERM_SPREAD d.DEF d.EQ_RETURN d.ROE d.DIVIDEND, vce(robust)
estimates store m7

estout m1 m2 m3 m4 m5 m6 m7   using ${path_table}NDI_NEW.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7   
* NEI **
reg d.nei d.SENTIMENT, vce(robust)
estimates store m1

reg d.nei d.NEWS, vce(robust)
estimates store m2

reg d.nei d.EQ_SENTIMENT, vce(robust)
estimates store m3

reg d.nei d.ANALYST, vce(robust)
estimates store m4

reg d.nei d.analy_disag, vce(robust)
estimates store m5

reg d.nei d.SENTIMENT d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag , vce(robust)
estimates store m6

reg d.nei d.SENTIMENT d.VIX d.SHORT_RATE d.TERM_SPREAD d.DEF d.EQ_RETURN d.ROE d.DIVIDEND, vce(robust)
estimates store m7

estout m1 m2 m3 m4 m5 m6 m7   using ${path_table}NEI_NEW.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7

* This is just predicting levels-- more in the spirit of MS 2017 paper
*reg d.ndi l.fitted_stata, vce(cluster firm)
*reg nei l.fitted_stata, vce(cluster firm)
 
* Predict changes in Financing mix: cross-section data
xtreg d.ndi d.SENTIMENT, be
estimates store m1

xtreg d.ndi d.NEWS, be
estimates store m2

xtreg d.ndi d.EQ_SENTIMENT, be
estimates store m3

xtreg d.ndi d.ANALYST, be
estimates store m4

xtreg d.ndi d.analy_disag, be
estimates store m5

xtreg d.ndi d.SENTIMENT d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag, be
estimates store m6

xtreg d.ndi d.SENTIMENT EQ_RETURN VIX d.SHORT_RATE d.TERM_SPREAD d.DEF d.ROE d.DIVIDEND, be
estimates store m7

estout m1 m2 m3 m4 m5 m6 m7   using ${path_table}NDI_CS_NEW.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7 

* NEI **
xtreg d.nei d.SENTIMENT, be
estimates store m1

xtreg d.nei d.NEWS, be
estimates store m2

xtreg d.nei d.EQ_SENTIMENT, be
estimates store m3

xtreg d.nei d.ANALYST, be
estimates store m4

xtreg d.nei d.analy_disag, be
estimates store m5

xtreg d.nei d.SENTIMENT d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag, be
estimates store m6

xtreg d.nei d.SENTIMENT EQ_RETURN VIX d.SHORT_RATE d.TERM_SPREAD d.DEF d.ROE d.DIVIDEND, be
estimates store m7

estout m1 m2 m3 m4 m5 m6 m7   using ${path_table}NEI_CS_NEW.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7   

* Predicting cross-sectional CDS returns with sentiment and proxies 
*xtreg d.cds l.fitted_stata l.av_sentiment l.analyst_rec2 l.analy_disag l.aver_news ,be
* in univariate reg my sentiment proxy beats all of them-- in multi only news is borderline significant

* Predict cross-sectional CDS returns with (my)sentiment & credit risk proxies controle variables


* Check the above if it holds in firm-specific + for rating categories 


* --------- Predict Investment with sentiment and other stuff....cross-section --------- ****


*reg d.TOT_INVEST_TO_ASSET d.SENTIMENT, vce(robust)
*estimates store m1

*reg d.TOT_INVEST_TO_ASSET d.NEWS, vce(robust)
*estimates store m2

*reg d.TOT_INVEST_TO_ASSET d.EQ_SENTIMENT, vce(robust)
*estimates store m3

*reg d.TOT_INVEST_TO_ASSET d.ANALYST, vce(robust)
*estimates store m4

*reg d.TOT_INVEST_TO_ASSET d.analy_disag, vce(robust)
*estimates store m5

*reg d.TOT_INVEST_TO_ASSET d.SENTIMENT d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag, vce(robust)
*estimates store m6

*reg d.TOT_INVEST_TO_ASSET d.SENTIMENT SP500 VIX d.SHORT_RATE d.TERM_SPREAD , vce(robust)
*estimates store m7

*reg d.TOT_INVEST_TO_ASSET d.SENTIMENT SP500 VIX d.SHORT_RATE d.TERM_SPREAD d.ROE d.DIVIDEND, vce(robust)
*estimates store m7

*reg d.TOT_INVEST_TO_ASSET d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag d.SENTIMENT SP500 VIX d.SHORT_RATE d.TERM_SPREAD d.ROE d.DIVIDEND, vce(robust)
*estimates store m8

*estout m1 m2 m3 m4 m5 m6 m7 m8  using ${path_table}INVEST.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7) (8)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
*drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7 _est_m8

* Cross-Section
xtreg d.TOT_INVEST_TO_ASSET d.SENTIMENT, be
estimates store m1

xtreg d.TOT_INVEST_TO_ASSET d.NEWS, be
estimates store m2

xtreg d.TOT_INVEST_TO_ASSET d.EQ_SENTIMENT, be
estimates store m3

xtreg d.TOT_INVEST_TO_ASSET d.ANALYST, be
estimates store m4

xtreg d.TOT_INVEST_TO_ASSET d.analy_disag, be
estimates store m5

xtreg d.TOT_INVEST_TO_ASSET d.SENTIMENT d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag, be
estimates store m6

xtreg d.TOT_INVEST_TO_ASSET d.SENTIMENT d.EQ_RETURN d.TOBINQ d.REALSALES,be
estimates store m7

xtreg d.TOT_INVEST_TO_ASSET d.SENTIMENT d.EQ_SENTIMENT SP500 VIX d.SHORT_RATE d.TERM_SPREAD d.DEF d.ROE d.DIVIDEND, be
estimates store m8

xtreg d.TOT_INVEST_TO_ASSET  d.SENTIMENT SP500 VIX d.SHORT_RATE d.TERM_SPREAD d.TOBINQ d.REALSALES d.DEF d.ROE d.DIVIDEND, be
estimates store m9

estout m1 m2 m3 m4 m5 m6 m7 m8 m9 using ${path_table}INVEST_CS.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7) (8) (9)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7 _est_m8 _est_m9



* --------------- Sentiment Predict GDP ----------
reg d.REALGDP d.SENTIMENT, vce(cluster firm)
estimates store m1

reg d.REALGDP d.NEWS, vce(cluster firm)
estimates store m2

reg d.REALGDP d.EQ_SENTIMENT, vce(cluster firm)
estimates store m3

reg d.REALGDP d.ANALYST, vce(cluster firm)
estimates store m4

reg d.REALGDP d.analy_disag, vce(cluster firm)
estimates store m5

reg d.REALGDP d.SENTIMENT d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag, vce(cluster firm)
estimates store m6

*reg d.REALGDP d.SENTIMENT SP500 VIX d.SHORT_RATE d.TERM_SPREAD , vce(cluster firm)
*estimates store m7

*reg d.REALGDP d.SENTIMENT d.l.REALGDP SP500 VIX d.SHORT_RATE d.TERM_SPREAD, vce(cluster firm)
*estimates store m7

reg d.REALGDP d.SENTIMENT d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag d.l.REALGDP SP500 VIX d.SHORT_RATE d.TERM_SPREAD d.ROE d.DIVIDEND, vce(cluster firm)
estimates store m7

reg d.REALGDP d.SENTIMENT  d.l.REALGDP SP500 VIX d.SHORT_RATE d.TERM_SPREAD d.DEF d.ROE d.DIVIDEND, vce(cluster firm)
estimates store m8

estout m1 m2 m3 m4 m5 m6 m7 m8  using ${path_table}GDP.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7) (8)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7  _est_m7 


* --------------- Sentiment Predict GDP Cross-Section ***FORGET ABOUT IT----------
xtreg d.REALGDP d.SENTIMENT, vce(robust)
estimates store m1

xtreg d.REALGDP d.NEWS, vce(robust)
estimates store m2

xtreg d.REALGDP d.EQ_SENTIMENT, vce(robust)
estimates store m3

xtreg d.REALGDP d.ANALYST, vce(robust)
estimates store m4

xtreg d.REALGDP d.analy_disag, vce(robust)
estimates store m5

xtreg d.REALGDP d.SENTIMENT d.NEWS d.EQ_SENTIMENT d.ANALYST d.analy_disag, vce(robust)
estimates store m6

reg d.REALGDP d.SENTIMENT SP500 d.l.REALGDP d.l.SHORT_RATE d.l.TERM_SPREAD l.CPI, vce(robust)
estimates store m7


estout m1 m2 m3 m4 m5 m6 m7 using ${path_table}GDP_CS.tex, cells(b( fmt(%9.3f)) t(par)) stats(r2 N, labels("R-squared" "N") fmt(%9.2f %9.0g) )  label collabels(, none) varlabels( "rv"	"rvc" "rvj"	"bad_jump_s" "good_jump_s" "h_vola_s" "h_skew_s" "h_kurt_s") varwidth(16) modelwidth(12) delimiter("&") mlabels((1) (2) (3) (4) (5) (6) (7)) prehead(\begin{tabular}{l*{@M}{c}} \hline \hline) postfoot(\hline \hline \end{tabular}) style(tex) substitute(_ \_) order() replace
drop _est_m1 _est_m2 _est_m3 _est_m4 _est_m5 _est_m6 _est_m7  

